package com.kzw.oa.comm.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kzw.core.bean.Msg;
import com.kzw.core.orm.Page;
import com.kzw.core.orm.PageRequest;
import com.kzw.core.orm.StringPropertyFilter;
import com.kzw.core.util.BeanUtil;
import com.kzw.core.util.ExtjsUtil;
import com.kzw.core.util.json.JSON;
import com.kzw.core.util.web.ResponseUtils;
import com.kzw.misc.utils.ContextUtil;
import com.kzw.oa.comm.model.TongBao;
import com.kzw.oa.comm.service.TongBaoService;
import com.kzw.oa.system.model.FileAttach;
import com.kzw.oa.system.service.FileAttachService;

@Controller
@RequestMapping("/comm/tongbao/")
public class TongBaoAction {
	@Autowired
	private TongBaoService tongBaoService;
	@Autowired
	private FileAttachService attachService;
	
	/**
	 * 显示我单位能显示的列表
	 */
	@RequestMapping("listMy")
	public void listMy(HttpServletRequest request, HttpServletResponse response){
		PageRequest pageRequest = ExtjsUtil.getPageReq(request);
		List<StringPropertyFilter> filters = ExtjsUtil.getFilters(request);
		
		List<TongBao> list = tongBaoService.listMy(filters);
		// 构造分页信息
		Page<TongBao> page = ExtjsUtil.toPageForExtJs(pageRequest, list);
		
		String json = new JSON(page).buildWithFilters(3);		
		ResponseUtils.renderJson(response, json);
	}
	
	/**
	 * 桌面显示：我单位能显示的列表
	 */
	@RequestMapping("desktop")
	public String desktop(HttpServletRequest request){
		PageRequest pageRequest = ExtjsUtil.getPageReq(request);
		List<StringPropertyFilter> filters = ExtjsUtil.getFilters(request);
		
		List<TongBao> list = tongBaoService.listMy(filters);
		Page<TongBao> page = ExtjsUtil.toPageForExtJs(pageRequest, list);
		
		request.setAttribute("page", page);
		return "home";
	}
	

	/**
	 * 显示列表
	 */
	@RequestMapping("list")
	public void list(HttpServletRequest request, HttpServletResponse response) {
		PageRequest pageRequest = ExtjsUtil.getPageReq(request);
		List<StringPropertyFilter> filters = ExtjsUtil.getFilters(request);
		Long depId = ContextUtil.getCurrentDept().getDepId();
		filters.add(new StringPropertyFilter("EQL_dept.depId", depId+""));
		
		Page<TongBao> page = tongBaoService.search2(pageRequest, filters);

		String json = new JSON(page).buildWithFilters(3);
		ResponseUtils.renderJson(response, json);
	}

	/**
	 * 批量删除
	 */
	@ResponseBody
	@RequestMapping("multiDel")
	public Msg multiDel(String[] ids) {
		tongBaoService.remove(ids);
		return new Msg();
	}

	/**
	 * 显示详细信息
	 */
	@ResponseBody
	@RequestMapping("get")
	public Msg get(Long oid) {
		TongBao tongBao = tongBaoService.get(oid);
		String json = new JSON(tongBao).buildWithFilters(2);
		return new Msg(true, 0, json);
	}
	
	/**
	 * 通知公告详情
	 * */
	@RequestMapping("info/{oid}")
	public String info(@PathVariable Long oid, Model model) {
		TongBao tongBao = tongBaoService.get(oid);
		model.addAttribute("tongbao", tongBao);
		return "comm/tongbao_info";
	}
	
	@RequestMapping("/detail")
	public String detail(Long oid, HttpServletRequest request) {
		String opt = request.getParameter("opt");
		TongBao tongBao = tongBaoService.get(oid);
		
		List<StringPropertyFilter> filters = new ArrayList<StringPropertyFilter>();
		List<TongBao> list = tongBaoService.listMy(filters);
		int index = 0;
		for(TongBao tb : list) {
			if(tb.getOid().equals(oid)) {
				break;
			}
			index++;
		}
		
		request.setAttribute("havePreFlag", "xx");
		request.setAttribute("haveNextFlag", "xx");
		if(index == 0) {
			request.setAttribute("havePreFlag", "endPre");
		}
		if(index == list.size() - 1) {
			request.setAttribute("haveNextFlag", "endNext");
		}
		
		if(StringUtils.isNotBlank(opt)) {
			if(opt.equals("_pre")) {
				tongBao = list.get(index - 1);
			} else if(opt.equals("_next")) {
				tongBao = list.get(index + 1);
			}
		}
		request.setAttribute("tongbao", tongBao);
		
		// 附件
		String fileIds = tongBao.getFiles();
		if (StringUtils.isNotEmpty(fileIds)) {
			fileIds = fileIds.substring(0, fileIds.length() - 1);
			List<FileAttach> attachs = attachService.findByHQL("from FileAttach where id in (" + fileIds + ")");
			request.setAttribute("attachs", attachs);
		}
		return "detail";
	}
	

	/**
	 * 添加及保存操作
	 */
	@ResponseBody
	@RequestMapping("save")
	public Msg save(TongBao tongBao) {
		if (tongBao.getOid() == null) {
			tongBao.setAuthor(ContextUtil.getCurrentUser());
			tongBao.setDept(ContextUtil.getCurrentDept());
			tongBaoService.saveOrUpdate(tongBao);
		} else {
			TongBao orgTongBao = tongBaoService.get(tongBao.getOid());
			try {
				BeanUtil.copyNotNullProperties(orgTongBao, tongBao);
				tongBaoService.saveOrUpdate(orgTongBao);
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
		return new Msg();
	}

	
	/**
	 * iframe页面
	 */
	@RequestMapping("html/add")
	public String addUi() {
		return "comm/tongbao_form";
	}
	
	@RequestMapping("html/edit")
	public String editUi(Long oid, Model model) {
		TongBao tongbao = tongBaoService.get(oid);
		model.addAttribute("tongbao", tongbao);
		return "comm/tongbao_form";
	}
	

}
